package com.baizhi.controller;

import com.baizhi.entity.Admin;
import com.baizhi.entity.AdminRole;
import com.baizhi.entity.RoleResource;
import com.baizhi.mapper.AdminMapper;
import com.baizhi.mapper.AdminRoleMapper;
import com.baizhi.service.AdminRoleService;
import com.baizhi.service.AdminService;
import com.baizhi.service.RoleResourceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Slf4j
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private  AdminService adminService;
    @Autowired
    private AdminRoleService adminRoleService;
    @Autowired
    private RoleResourceService roleResourceService;
    @RequestMapping("/login")
    public @ResponseBody Map login(String username, String password, String code, HttpServletRequest request){
        Map map = new HashMap();
        HttpSession session = request.getSession();
        String realCode = (String) session.getAttribute("code");
        if(code==null||!code.equalsIgnoreCase(realCode)){
            map.put("status","failed");
            map.put("msg","验证码错误");
        }else{
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password);
            Subject subject = SecurityUtils.getSubject();
            try{
                subject.login(usernamePasswordToken);
                session.setAttribute("username",username);
                map.put("status","success");
            }catch(Exception e){
                map.put("status","failed");
                map.put("msg","用户名或密码错误");
            }
        }
        return map;
    }
    @RequestMapping("/addAdmin")
    public @ResponseBody Map addAdmin(String username,String password){
        Map map = new HashMap();
        if(adminService.selectByName(username)==null){
            //        1.生成随机盐值
            String salt = UUID.randomUUID().toString().substring(0, 4);
            log.info(salt+"");
            //        2.加密
            Md5Hash md5Hash = new Md5Hash(password,salt,1024);
            password = md5Hash.toHex();
            //        3.添加数据库
            Admin admin = new Admin();
            admin.setUserName(username);
            admin.setPassword(password);
            admin.setSalt(salt);
            try{
                adminMapper.insert(admin);
                map.put("status","success");
            }catch(Exception e){
                map.put("status","failed");
                map.put("msg","注册失败，请稍后再试");
            }

        }else{
            map.put("status","failed");
            map.put("msg","用户已存在");
        }
        return map;
    }
    @RequestMapping("/showAdmin")
    @ResponseBody
    public Map showAllAdmin(){
        List<AdminRole> adminRoles = adminRoleService.selectAll();
        Map map = new HashMap();
        map.put("code",0);
        map.put("data",adminRoles);
        return map;
    }
    @RequestMapping("/showRole")
    @ResponseBody
    public Map showAllRole(){
        List<RoleResource> roleResources = roleResourceService.selectAll();
        Map map = new HashMap();
        map.put("code",0);
        map.put("data",roleResources);
        return map;
    }
}
